package com.clg.common.tools;

import com.clg.common.beans.KeyValue;
import com.clg.common.constants.Constants;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import java.util.ArrayList;
import java.util.List;

/**
 * 本方法校验失败后会将所有错误信息
 * 设置到 Constants.ERRORS_KEY 如果errKey等于inputName，可以在前端对应name的输入框显示
 * Created by usercai on 2016/9/29.
 */
public abstract class AppValidator extends Validator {

    /**
     * Use validateXxx method to validate the parameters of this action.
     */
    protected abstract void validate(Controller c);

    /**
     * Handle the validate error.
     * Example:<br>
     * controller.keepPara();<br>
     * controller.render("register.html");
     */
    protected abstract void handleError_(Controller c);

    protected List<KeyValue> validateErrs = new ArrayList();


    protected void handleError(Controller c){
        c.setAttr(Constants.ERRORS_KEY,validateErrs);
        handleError_(c);
    };

    @Override
    protected void addError(String errorKey, String errorMessage) {
        super.addError(errorKey, errorMessage);
        validateErrs.add(new KeyValue(errorKey,errorMessage));
    }


}

